﻿// Decompiled with JetBrains decompiler
// Type: NetickEditor.NetickEditorProperty
// Assembly: Netick.Unity.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0658BB4A-05C4-428D-9BD6-82E94959F5A1
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Netick.Unity.Editor.dll

using System;
using UnityEditor;
using UnityEngine;

#nullable disable
namespace NetickEditor;

internal class NetickEditorProperty
{
  public static void DrawProperty(
    UnityEngine.Object behaviour,
    string title,
    SerializedProperty property,
    string helpText = "")
  {
    NetickEditorProperty.PropertyDrawWithName method = new NetickEditorProperty.PropertyDrawWithName()
    {
      Property = property
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText, property);
  }

  public static void DrawProperty(UnityEngine.Object behaviour, SerializedProperty property, string helpText = "")
  {
    NetickEditorProperty.PropertyDraw method = new NetickEditorProperty.PropertyDraw()
    {
      Property = property
    };
    NetickEditorProperty.DrawInternal(behaviour, property.name, (NetickEditorProperty.DrawMethod) method, helpText, property);
  }

  public static void DrawField(
    UnityEngine.Object behaviour,
    string title,
    string text,
    bool hasHelpButton,
    string helpText = "")
  {
    NetickEditorProperty.LabelDraw method = new NetickEditorProperty.LabelDraw()
    {
      Text = text,
      ToolTipText = helpText,
      HasHelpButton = hasHelpButton
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText, helpButton: hasHelpButton);
  }

  public static void DrawLabel(UnityEngine.Object behaviour, string title, string text, string helpText = "")
  {
    NetickEditorProperty.LabelDraw method = new NetickEditorProperty.LabelDraw()
    {
      Text = text,
      ToolTipText = helpText
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText);
  }

  public static void DrawIntSlider(
    UnityEngine.Object behaviour,
    SerializedProperty property,
    string title,
    int max,
    int min,
    string helpText = "")
  {
    NetickEditorProperty.SliderDraw method = new NetickEditorProperty.SliderDraw()
    {
      Property = property,
      UseInt = true,
      Max = (float) max,
      Min = (float) min
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText);
  }

  public static void DrawSlider(
    UnityEngine.Object behaviour,
    SerializedProperty property,
    string title,
    float max,
    float min,
    string helpText = "")
  {
    NetickEditorProperty.SliderDraw method = new NetickEditorProperty.SliderDraw()
    {
      Property = property,
      UseInt = false,
      Max = max,
      Min = min
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText);
  }

  public static void DrawSendRateSlider(
    UnityEngine.Object behaviour,
    SerializedProperty property,
    string title,
    string helpText = "")
  {
    NetickEditorProperty.SendRateSlider method = new NetickEditorProperty.SendRateSlider()
    {
      Property = property
    };
    NetickEditorProperty.DrawInternal(behaviour, title, (NetickEditorProperty.DrawMethod) method, helpText);
  }

  private static object DrawInternal(
    UnityEngine.Object behaviour,
    string title,
    NetickEditorProperty.DrawMethod method,
    string helpText,
    SerializedProperty property = null,
    bool helpButton = true)
  {
    if (helpText == "")
      helpText = method.ToolTip();
    string str1 = behaviour.GetInstanceID().ToString();
    string str2 = title + str1;
    bool flag = NetickEditorToggleState.SelectedPropertyName == str2;
    Color backgroundColor = GUI.backgroundColor;
    Vector3 vector3 = new Vector3(103f, 122f, 138f);
    Color color = new Color(vector3.x / (float) byte.MaxValue, vector3.y / (float) byte.MaxValue, vector3.z / (float) byte.MaxValue);
    EditorGUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
    if (helpButton)
    {
      EditorGUI.BeginDisabledGroup(helpText == "" || helpText == null);
      GUI.backgroundColor = !EditorGUIUtility.isProSkin ? (flag ? Color.black * 0.2f : Color.white) : (flag ? Color.grey : color);
      if (GUILayout.Button(flag ? "▼" : "?", GUILayout.Width(17f), GUILayout.Height(17f)))
        NetickEditorToggleState.SelectedPropertyName = !flag ? str2 : "";
      GUI.backgroundColor = backgroundColor;
      EditorGUI.EndDisabledGroup();
    }
    object obj = method.Draw(title);
    EditorGUILayout.EndHorizontal();
    if (!(NetickEditorToggleState.SelectedPropertyName == str2))
      return obj;
    GUIStyle style = GUI.skin.GetStyle("HelpBox");
    style.richText = true;
    style.margin.bottom = 5;
    style.border.top = 15;
    style.border.bottom = 15;
    style.border.left = 15;
    style.border.right = 15;
    style.fontSize = 12;
    style.padding = new RectOffset(10, 10, 10, 10);
    EditorGUILayout.LabelField(helpText, style, Array.Empty<GUILayoutOption>());
    return obj;
  }

  private interface DrawMethod
  {
    object Draw(string title);

    string ToolTip();
  }

  private struct LabelDraw : NetickEditorProperty.DrawMethod
  {
    public string Text;
    public string ToolTipText;
    public bool HasHelpButton;

    public object Draw(string title)
    {
      EditorGUILayout.LabelField(title, this.Text, Array.Empty<GUILayoutOption>());
      return (object) null;
    }

    public string ToolTip() => this.ToolTipText;
  }

  private struct PropertyDraw : NetickEditorProperty.DrawMethod
  {
    public SerializedProperty Property;

    public object Draw(string title)
    {
      EditorGUILayout.PropertyField(this.Property, Array.Empty<GUILayoutOption>());
      return (object) null;
    }

    public string ToolTip() => this.Property.tooltip;
  }

  private struct PropertyDrawWithName : NetickEditorProperty.DrawMethod
  {
    public SerializedProperty Property;

    public object Draw(string title)
    {
      EditorGUILayout.PropertyField(this.Property, new GUIContent(title), Array.Empty<GUILayoutOption>());
      return (object) null;
    }

    public string ToolTip() => this.Property.tooltip;
  }

  private struct SliderDraw : NetickEditorProperty.DrawMethod
  {
    public bool UseInt;
    public float Max;
    public float Min;
    public SerializedProperty Property;

    public object Draw(string title)
    {
      if (!this.UseInt)
        this.Property.floatValue = EditorGUILayout.Slider(title, this.Property.floatValue, this.Min, this.Max, Array.Empty<GUILayoutOption>());
      else
        this.Property.intValue = EditorGUILayout.IntSlider(title, this.Property.intValue, (int) this.Min, (int) this.Max, Array.Empty<GUILayoutOption>());
      return (object) null;
    }

    public string ToolTip() => this.Property.tooltip;
  }

  private struct SendRateSlider : NetickEditorProperty.DrawMethod
  {
    public SerializedProperty Property;

    public object Draw(string title)
    {
      EditorGUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
      this.Property.intValue = EditorGUILayout.IntSlider(title, this.Property.intValue, 5, 1, Array.Empty<GUILayoutOption>());
      GUILayout.Label((1f / NetworkConfigEditor.TickPeriod / (float) this.Property.intValue).ToString());
      EditorGUILayout.EndHorizontal();
      return (object) null;
    }

    public string ToolTip() => this.Property.tooltip;
  }
}
